package com.bos.controller.json;

import com.bos.dao.BankDAO;
import com.bos.domain.Bank;
import com.bos.editor.BankEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Latief
 * Date: 3/17/11
 * Time: 8:29 PM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class BankJsonController {

    private BankDAO bankDAO;

/* --------------------- Getter and Setter ---------------------*/

    @Autowired
    public void setBankDAO(BankDAO bankDAO) {
        this.bankDAO = bankDAO;
    }

    /**
     * ************** JSON **************************************************************************
     */

    @RequestMapping(value = "/bank.json", method = RequestMethod.POST)
    public
    @ResponseBody
    Map<String, Object> save(@RequestBody Bank bank) {
        if (bank == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            bankDAO.save(bank);
            result.put("success", true);
            result.put("bank", bank);
            result.put("message", "Sukses Save Bank " + bank);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("bank", bank);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Save Bank " + bank);
        }

        return result;
    }

    @RequestMapping(value = "/bank/{id}/delete.json", method = RequestMethod.GET)
    public
    @ResponseBody
    Map<String, Object> delete(@PathVariable("id") Bank bank) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            bankDAO.delete(bank);
            result.put("success", true);
            result.put("message", "Sukses Delete Bank " + bank);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal Delete Bank " + bank);
        }
        return result;
    }

    @RequestMapping(value = "/bank.json", method = RequestMethod.PUT)
    public
    @ResponseBody
    Map<String, Object> update(@RequestBody Bank bank) {
        if (bank == null) throw new IllegalArgumentException("A Product is required");
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            bankDAO.merge(bank);
            result.put("success", true);
            result.put("bank", bank);
            result.put("message", "Sukses Update Bank " + bank);
        } catch (DataAccessException e) {
            result.put("success", false);
            result.put("bank", bank);
            result.put("exception", e.getMessage());
            result.put("message", "Gagal update bank " + bank);
        }
        return result;
    }

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        binder.registerCustomEditor(Bank.class, new BankEditor(bankDAO));
    }

}
